package com.ronghuanet.gift.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ronghuanet.gift.client.SmsValidateCodeClient;
import com.ronghuanet.gift.clients.LoginUserClient;
import com.ronghuanet.gift.domain.LoginUser;
import com.ronghuanet.gift.domain.User;
import com.ronghuanet.gift.dto.PhoneRegisterDto;
import com.ronghuanet.gift.dto.RegisterPhoneValidateDto;
import com.ronghuanet.gift.exception.asserts.Assert;
import com.ronghuanet.gift.exception.code.StatusCode;
import com.ronghuanet.gift.mapper.UserMapper;
import com.ronghuanet.gift.service.IUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ronghuanet.gift.util.AjaxResult;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author yaosang
 * @since 2022-10-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private SmsValidateCodeClient smsValidateCodeClient;

    @Autowired
    private LoginUserClient loginUserClient;
    @Override
    @GlobalTransactional
    public  AjaxResult registerByPhone(PhoneRegisterDto phoneRegisterDto) {
        String phone = phoneRegisterDto.getPhone();
        String password = phoneRegisterDto.getPassword();
        //1 校验
        //1.1 jsr303 null校验
        //1.2 phone注册校验
        List<User> users = userMapper.selectList(new EntityWrapper<User>().eq("phone", phone));
        Assert.assertListIsEmpty(users, StatusCode.CODE_400_USER_EXSIT);
        //1.3 验证码校验
        RegisterPhoneValidateDto dto = new RegisterPhoneValidateDto();
        dto.setPhone(phone);
        dto.setValidateCode(phoneRegisterDto.getValidateCode());
        AjaxResult ajaxResult = smsValidateCodeClient
                .validateRegisterCode(dto);
        if (!ajaxResult.isSuccess()) //如果没有校验通过，直接返回错误
            return ajaxResult;

        //2 注册
        //2.1 loginUser
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(UUID.randomUUID().toString());
        loginUser.setPassword(password);
        loginUser.setTel(phone);
        loginUser.setType(1);
        AjaxResult result = loginUserClient.addOrUpdate(loginUser);
        if (!result.isSuccess())
            return result;

        Long loginUserId = Long.valueOf(result.getResultObj().toString());
        //2.2 user
        User user = new User();
        user.setCreateTime(new Date());
        user.setPhone(phone);
        user.setPassword(password);
        user.setSalt(null);
        user.setBitState(1L);
        user.setSecLevel(1);
        user.setLoginId(loginUserId);
        user.setUsername(loginUser.getUsername());
        userMapper.insert(user);

        return AjaxResult.me();
    }

}
